#include <stdio.h>
#include <string.h>
#define MAXSIZE 1024
typedef struct SeqString
{
    char data[MAXSIZE];
    int curlen;
} SeqString;

SeqString *InitSeqString()
{
    SeqString *s = NULL;
    s->data[0] = '\0';
    s->curlen = 0;
    return s;
}

void StrConcat(char *s1, char *s2, char *s)
{
    int j, k;
    j = k = 0;
    while (s1[j] != '\0')
    {
        s[k++] = s1[j++];
    }

    j = 0;

    while (s2[j] != '\0')
    {
        s[k++] = s2[j++];
    }
    s[k] = '\0';
}

int SubString(char *s, char *sub, int pos, int length)
{
    int i;

    if (pos < 0)
    {
        return 0;
    }

    length += pos;

    for (i = pos; i < length; i++)
    {
        sub[i] = s[i];
    }

    sub[length] = '\0';

    return 1;
}

int StringCompare(char *s1, char *s2)
{
    int j = 0;
    while (s1[j] == s2[j] && s1[j] != '\0')
    {
        j++;
    }
    return s1[j] - s2[j];
}

int Index_BF(char *s, char *t)
{
    int i, j, flag;

    i = j = flag = 0;


    while(i < strlen(s) && j < strlen(t))
    {
        if(s[i] == t[j])
        {
            i++;
            j++;
        }
        else
        {
            j = 0;
            i = flag;
            flag++;
        }
    }

    if (j == strlen(t))
    {
        printf("reuslt: %d\n", i - strlen(t));
        return i - strlen(t);
    }
    return -1 ;
}

int main()
{
    char *str1 = "hello ";
    char *str2 = "world";
    char str[20];
    char msg[20];
    int result;

    StrConcat(str1, str2, str);
    printf("StrConcat:%s\n", str);

    SubString(str, msg, 6, 5);
    printf("SubString:%s\n", msg);

    result = StringCompare(str, "hello world");
    if (result == 0)
    {
        printf("Successful match!\n");
    }
    else
    {
        printf("failed:%d\n", result);
    }

    result = Index_BF("ababcabcacbab", "abcac");
    if (result)
    {
        printf("Successfully found,Index at: %d\n", result);
    }
    return 0;
}